/*	kd:
	This is about the layout of the shop. We assume that only one shop can be
	displayed at a time. You have to deal with things like highlighting, the
	transition animation and the correct item selection based on the cursor
	position here. */

/*	kd:
	The ring inventory is an art form to us at this point. */

class Gcs_RingLayout : Gcs_InvLayout {
	const ring_max_rad_sq       = 0.431f * 0.431f;
	const ring_min_rad_sq       = 0.135f * 0.135f;
	
	const outer_draw_rad        = 0.30666f;
	const highlight_draw_rad    = 0.30777f;
	const inner_draw_rad        = 0.0f;
	const d_draw_rad            = outer_draw_rad - inner_draw_rad;
	const offset_len            = 0.035f;
	
	const rotation_amount       = 130.0f;
	
	// For this layout, we assume item 0 is at 12 o'clock, then, in the same
	// clock intuition... we go anti-clockwise because mathematicians enjoy
	// your suffering. It makes us young.
	override uint SelectItem (float x, float y) const {
		// You're reading this correctly. Assume x, y is rotated 90° clockwise.
		// Additionally, zs y-axis is oriented down. It's odd but it works.
		float dx = center_x - x;
		float dy = center_y - y;
		[dx, dy] = Gcs_ScreenCoords.AspectCoord(dx, dy);
		
		float ang_2 = 180.0f / max(1, m_size);
		float ang_select = (atan2(dx, dy) + ang_2) % 360.0f;
		
		/* Clamp call not needed, but we just know better. */
		return clamp(m_size * ang_select / 360.0f, 0, m_size);
	}
	
	override bool CloseEnough (float x, float y) const {
		float dx = x - center_x;
		float dy = y - center_y;
		[dx, dy] = Gcs_ScreenCoords.AspectCoord(dx, dy);
		[dx, dy] = Gcs_ScreenCoords.ScaleDiffToWidth(dx, dy, 720);
		
		float len_sq = dx * dx + dy * dy;
		
		if(ring_min_rad_sq <= len_sq && len_sq <= ring_max_rad_sq) {
			return true;
		}
		
		return false;
	}
	
	override void OutInvPositions (float t, float t_rad, float rot_f) {
		float ang_per_item = 360.0f / max(1, m_size);
		float ipol = Gc_Interpol.trerp_unit(t);
		float ang = rot_f * rotation_amount * (ipol - 1) - 90.0f;
		float len = inner_draw_rad + d_draw_rad * ipol * t_rad;
		
		for(uint i = 0; i < m_size; i++) {
			m_pos_x [i] = len * cos(ang);
			m_pos_y [i] = len * sin(ang);
			ang += ang_per_item;
		}
	}
	
	override void AddOffsets (Gcs_LinearFades fades) {
		uint size = min(m_size, fades.m_size);
		float ang_per_item = 360.0f / max(1, size);
		float ang = 270;
		float t;
		
		for(uint i = 0; i < size; i++) {
			t = fades.Current(i);
			m_pos_x [i] += offset_len * cos(ang) * t;
			m_pos_y [i] += offset_len * sin(ang) * t;
			ang += ang_per_item;
		}
	}
}

/*	kd:
	This exists to output the draw coordinates for the shop items and as an
	interface to selecting an item in that layout. Here:
	
	(0 0) is top left
	(1 1) is bottom right
	
	No matter the resolution, linearly. That means during output, aspect ratio
	had to be taken into consideration to make circles look like circles,
	for example. */

class Gcs_InvLayout play {
	
	// vector2 m_old_pos [Gc_Shop_Inv.max_items_per_shop];
	// vector2 m_pos     [Gc_Shop_Inv.max_items_per_shop];
	Array <float>   m_old_pos_x;
	Array <float>   m_old_pos_y;
	Array <float>   m_off_x;
	Array <float>   m_off_y;
	Array <float>   m_pos_x;
	Array <float>   m_pos_y;
	uint m_size;
	
	float center_x;
	float center_y;
	
	void SetCenter (float cx = 0.5, float cy = 0.415) {
		center_x = cx;
		center_y = cy;
	}
	
	/*	Basically init. The idea is that at specific points in time, the shop
		changes and from then onwards, we know exactly how much memory we need.
		When the shop changes, doing many ops will be hardly noticeable. */
	
	void ClearAllReserve (uint size) {
		m_old_pos_x.	Clear();
		m_old_pos_y.	Clear();
		m_off_x.		Clear();
		m_off_y.		Clear();
		m_pos_x.		Clear();
		m_pos_y.		Clear();
		
		m_old_pos_x.	Reserve(size);
		m_old_pos_y.	Reserve(size);
		m_off_x.		Reserve(size);
		m_off_y.		Reserve(size);
		m_pos_x.		Reserve(size);
		m_pos_y.		Reserve(size);
		m_size = size;
	}
	
	/*	For interpolation purposes, the old position is kept as well as the
		current state's position. */
	
	void UpdateOldPositions () {
		for(uint i = 0; i < m_size; i++) {
			m_old_pos_x [i] = m_pos_x [i];
			m_old_pos_y [i] = m_pos_y [i];
		}
	}
	
	/*	The items are not individual buttons. That would eat processing time.
		Instead, we get the item index from an easy calculation like this. */
	virtual uint SelectItem (float x, float y) const {
		return 0;
	}
	
	virtual bool CloseEnough (float x, float y) const {
		return false;
	}
	
	virtual bool TooClose (float x, float y) const {
		return false;
	}
	
	/* Outputs draw coordinates. */
	virtual void OutInvPositions (float t, float scale = 1, float rot_f = 1) {}
	
	/*	Offset direction (!) for when you want to mark a specific item. The
		array t is how far out the button should be. In practice, you pass
		m_t found in Gcs_LinearFades.
		
		Sanity tip: Condition t so all values are in the [-1, 1] range. */
	virtual void AddOffsets (Gcs_LinearFades fades) {}
	
	// We put output here, since polling this class every frac-tic would be
	// stupidly costly and for some reason, zs cannot pass or return arrays.
	ui void DrawAll (RenderEvent event, Gcs_DrawCache cache,
		Gcs_LinearFades fades, Gcs_ButtonSprites sprites,
		float base_alpha = 0.5) const {
		float   t      = event.fractic;
		float   t_ctr  = 1 - t;
		float   scale  = cache.m_scale;
		
		float aspect   = Screen.GetAspectRatio();
		float height   = 720 / scale;
		float width    = height * aspect;
		
		TextureId darken = sprites.Darken();
		TextureId equip_light = sprites.EquipOverlay();
		
		for(uint i = 0; i < m_size; i++) {
			/* Uncapped fps interpolation: */
			float pos_x = t_ctr * m_old_pos_x [i] + t * m_pos_x [i];
			float pos_y = t_ctr * m_old_pos_y [i] + t * m_pos_y [i];
			float current_alpha = fades.Current(i);
			
			/*	TODO: Move this out and possibly cache these positions, or maybe
				do it immediately in the OutInvPositions functions. */
			
			/*	Wide format. */
			if(1 < aspect) {
				pos_x = width / aspect * (center_x * aspect - pos_x);
				pos_y = height * (pos_y + center_y);
			}
			
			/*	High format. */
			else {
				pos_x = width * (center_x - pos_x);
				pos_y = height * aspect * (pos_y + center_y / aspect);
			}
			
			/* Output to screen: */
			Screen.DrawTexture(cache.m_button_sprite [i], true, pos_x, pos_y,
				DTA_CENTEROFFSET, true,
				DTA_KEEPRATIO, true,
				DTA_ALPHA, base_alpha,
				DTA_VIRTUALWIDTHF, width,
				DTA_VIRTUALHEIGHTF, height);
			Screen.DrawTexture(cache.m_highlighted_sprite [i], true, pos_x, pos_y,
				DTA_CENTEROFFSET, true,
				DTA_KEEPRATIO, true,
				DTA_ALPHA, current_alpha,
				DTA_VIRTUALWIDTHF, width,
				DTA_VIRTUALHEIGHTF, height);
			Screen.DrawTexture(cache.m_button_icon   [i], true, pos_x, pos_y,
				DTA_CENTEROFFSET, true,
				DTA_KEEPRATIO, true,
				DTA_ALPHA, base_alpha + current_alpha,
				DTA_VIRTUALWIDTHF, width,
				DTA_VIRTUALHEIGHTF, height);
			
			/*	Equip ring. */
			float alpha = cache.m_equip_alpha [i];
			
			Screen.DrawTexture(equip_light, true, pos_x, pos_y,
				DTA_CENTEROFFSET, true,
				DTA_KEEPRATIO, true,
				DTA_LEGACYRENDERSTYLE, STYLE_ADDSTENCIL,
				DTA_ALPHA,
					(base_alpha * alpha) * (1 - current_alpha) +
					alpha * current_alpha,
				DTA_FILLCOLOR, sprites.equip_ring_tint,
				DTA_VIRTUALWIDTHF, width,
				DTA_VIRTUALHEIGHTF, height);
			
			/*	Darkness. */
			alpha = cache.m_darken [i];
			
			Screen.DrawTexture(darken, true, pos_x, pos_y,
				DTA_CENTEROFFSET, true,
				DTA_KEEPRATIO, true,
				DTA_ALPHA,
					(base_alpha * alpha) * (1 - current_alpha) +
					alpha * current_alpha,
				DTA_VIRTUALWIDTHF, width,
				DTA_VIRTUALHEIGHTF, height);
		}
	}
	
	/*	Special version that blends two alpha values just right to produce
		the desired alpha whilst blending between two variations of the
		shop ring graphics smoothly.
		
		This is more difficult than it sounds.
		
		Also, zs draw calls aren't very pretty LOOKING, but they do exactly
		what they need to do here, and wrapping the calls won't exactly make it
		any better, so... */
	
	ui void DrawBlended(RenderEvent event, Gcs_SmoothCache cache, Gcs_LinearFades fades,
		Gcs_ButtonSprites sprites,
		float total_alpha = 1.0, float fade_alpha = 0.0) const {
		// float out_alpha = 1.0, float in_alpha = 0.0) const {
		float   t      = event.fractic;
		float   t_ctr  = 1 - t;
		float   scale  = cache.m_start.m_scale;
		
		float aspect   = Screen.GetAspectRatio();
		float height   = 720 / scale;
		float width    = height * aspect;
		
		TextureId darken = sprites.Darken();
		TextureId equip_light = sprites.EquipOverlay();
		
		/*	Blend two alpha values such that when you draw the same sprite twice
			with these two alpha values, their effective alpha is total_alpha.
			
			Note that taking half of total_alpha doesn't do the trick! */
		
		float in_alpha    = fade_alpha * total_alpha;
		float out_alpha   = (in_alpha - total_alpha) / (in_alpha - 1);
		
		for(uint i = 0; i < m_size; i++) {
			/* Uncapped fps interpolation: */
			float pos_x = t_ctr * m_old_pos_x [i] + t * m_pos_x [i];
			float pos_y = t_ctr * m_old_pos_y [i] + t * m_pos_y [i];
			float current_alpha = fades.Current(i);
			
			/* Fading in alpha for equip ring and "can't buy"-darkness. */
			float equip_alpha =
				cache.m_end.m_equip_alpha   [i] * fade_alpha +
				cache.m_start.m_equip_alpha [i] * (1 - fade_alpha);
			float darken_alpha =
				cache.m_end.m_darken   [i] * fade_alpha +
				cache.m_start.m_darken [i] * (1 - fade_alpha);
			
			/*	TODO: Move this out and possibly cache these positions, or maybe
				do it immediately in the OutInvPositions functions. */
			
			/*	Wide format. */
			if(1 < aspect) {
				pos_x = width / aspect * (center_x * aspect - pos_x);
				pos_y = height * (pos_y + center_y);
			}
			
			/*	High format. */
			else {
				pos_x = width * (center_x - pos_x);
				pos_y = height * aspect * (pos_y + center_y / aspect);
			}
			
			/* Output to screen: */
			Screen.DrawTexture(cache.m_start.m_button_sprite [i], true, pos_x, pos_y,
				DTA_CENTEROFFSET, true,
				DTA_KEEPRATIO, true,
				DTA_ALPHA, out_alpha,
				DTA_VIRTUALWIDTHF, width,
				DTA_VIRTUALHEIGHTF, height);
			Screen.DrawTexture(cache.m_end.m_button_sprite [i], true, pos_x, pos_y,
				DTA_CENTEROFFSET, true,
				DTA_KEEPRATIO, true,
				DTA_ALPHA, in_alpha,
				DTA_VIRTUALWIDTHF, width,
				DTA_VIRTUALHEIGHTF, height);
			
			Screen.DrawTexture(cache.m_start.m_highlighted_sprite [i], true, pos_x, pos_y,
				DTA_CENTEROFFSET, true,
				DTA_KEEPRATIO, true,
				DTA_ALPHA, current_alpha,
				DTA_VIRTUALWIDTHF, width,
				DTA_VIRTUALHEIGHTF, height);
			Screen.DrawTexture(cache.m_end.m_highlighted_sprite [i], true, pos_x, pos_y,
				DTA_CENTEROFFSET, true,
				DTA_KEEPRATIO, true,
				DTA_ALPHA, current_alpha * fade_alpha,
				DTA_VIRTUALWIDTHF, width,
				DTA_VIRTUALHEIGHTF, height);
			
			Screen.DrawTexture(cache.m_start.m_button_icon   [i], true, pos_x, pos_y,
				DTA_CENTEROFFSET, true,
				DTA_KEEPRATIO, true,
				DTA_ALPHA, out_alpha + current_alpha,
				DTA_VIRTUALWIDTHF, width,
				DTA_VIRTUALHEIGHTF, height);
			Screen.DrawTexture(cache.m_end.m_button_icon   [i], true, pos_x, pos_y,
				DTA_CENTEROFFSET, true,
				DTA_KEEPRATIO, true,
				DTA_ALPHA, in_alpha + current_alpha,
				DTA_VIRTUALWIDTHF, width,
				DTA_VIRTUALHEIGHTF, height);
			
			/*	Equip ring. */
			Screen.DrawTexture(equip_light, true, pos_x, pos_y,
				DTA_CENTEROFFSET, true,
				DTA_KEEPRATIO, true,
				DTA_LEGACYRENDERSTYLE, STYLE_ADDSTENCIL,
				DTA_ALPHA,
					(total_alpha * equip_alpha) * (1 - current_alpha) +
					equip_alpha * current_alpha,
				DTA_FILLCOLOR, sprites.equip_ring_tint,
				DTA_VIRTUALWIDTHF, width,
				DTA_VIRTUALHEIGHTF, height);
			
			/*	Darkness. */
			Screen.DrawTexture(darken, true, pos_x, pos_y,
				DTA_CENTEROFFSET, true,
				DTA_KEEPRATIO, true,
				// DTA_ALPHA, out_alpha * cache.m_start.m_darken [i],
				DTA_ALPHA,
					(total_alpha * darken_alpha) * (1 - current_alpha) +
					darken_alpha * current_alpha,
				DTA_VIRTUALWIDTHF, width,
				DTA_VIRTUALHEIGHTF, height);
		}
	}
	
	/* ui void DrawSelectables (
		RenderEvent			event,
		Gcq_SelectDrawCache	cache,
		Gcs_LinearFades		fades,
		Gcs_ButtonSprites	sprites,
		float				base_alpha = 0.5) const {
		
		float   t      = event.fractic;
		float   t_ctr  = 1 - t;
		float   scale  = cache.IconScale();
		
		float aspect   = Screen.GetAspectRatio();
		float height   = 720 / scale;
		float width    = height * aspect;
		
		for(uint i = 0; i < cache.size; i++) {
			// Interpolation between neighbouring frames:
			float pos_x = t_ctr * m_old_pos_x [i] + t * m_pos_x [i];
			float pos_y = t_ctr * m_old_pos_y [i] + t * m_pos_y [i];
			float current_alpha = fades.Current(i);
			
			// Wide format:
			if(1 < aspect) {
				pos_x = width / aspect * (center_x * aspect - pos_x);
				pos_y = height * (pos_y + center_y);
			}
			
			// High format:
			else {
				pos_x = width * (center_x - pos_x);
				pos_y = height * aspect * (pos_y + center_y / aspect);
			}
			
			// Draw:
			Screen.DrawTexture(cache.sprites [i], true, pos_x, pos_y,
				DTA_CENTEROFFSET,		true,
				DTA_KEEPRATIO,			true,
				DTA_ALPHA,				base_alpha + current_alpha,
				DTA_VIRTUALWIDTHF,		width,
				DTA_VIRTUALHEIGHTF,		height);
		}
	}*/
}